package com.atom.common.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.atom.common.exception.AvensNullPointerException;

public class StringUtil {
  
  private static Logger logger = Logger.getLogger(StringUtil.class);

  private static final String COMMA = ",";
  
  private static final String SEMICOLON = ";";

  /**
   * Description: 将用逗号连接的字符串分隔成数组
   * @param str
   * @return
   */
  public static String[] splitByComma(String str) {
    logger.info("Str : " + str);
    if (str == null || str.isEmpty()) {
      String[] a = {};
      return a;
    }
    if (str.equals(COMMA)) {
      String[] a = {""};
      return a;
    }    
    if (str.endsWith(COMMA)) {
      str = str.substring(0, str.length() - 1);
    }    
    return StringUtils.splitByWholeSeparatorPreserveAllTokens(str, COMMA);
  }
  
  public static String[] splitBySemicolon(String str) {
    if (str == null) {
      throw new AvensNullPointerException("string is null");
    }
    str.trim();
    if (str.endsWith(SEMICOLON)) {
      str.substring(0, str.length() - 1);
    }
    logger.info("Str : " + str);
    return str.split(SEMICOLON);
  }
  
  public static boolean hasLength(String str) {
    if (str == null || str.isEmpty()) {
      return false;
    }
    return true;
  }
  
  public static boolean hasText(String str) {
    if (!hasLength(str)) {
        return false;
    }
    int strLen = str.length();
    for (int i = 0; i < strLen; i++) {
        if (!Character.isWhitespace(str.charAt(i))) {
            return true;
        }
    }
    return false;
}
  
  public static void main(String[] args) {
    String[] ss = {"", ",", ",,", ",0,", "0,1,"};
    for (int i=0; i<ss.length; i++) {
      String[] s = splitByComma(ss[i]);
      System.out.println(ss[i]);
      System.out.println(s.length);
    }
  }

}
